package com.qk.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qk.dto.course.CourseDTO;
import com.qk.entity.Course;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import com.qk.common.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 课程服务实现类
 *
 * @author sch
 * @version 1.0
 * @since 2025-10-09-10:42
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;


    @Override
    public void addCourse(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.insert(course);
    }

    @Override
    public void deleteCourse(Integer id) {
        courseMapper.deleteById(id);
    }

    @Override
    public Course getCourseById(Integer id) {
        return courseMapper.selectById(id);
    }

    @Override
    public void updateCourse(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.updateById(course);
    }

    @Override
    public PageResult<Course> listCourse(CourseDTO courseDTO) {
        PageHelper.startPage(courseDTO.getPage(), courseDTO.getPageSize());
        //List<Course> courses = courseMapper.selectByCondition(courseDTO);
        // 执行查询
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(courseDTO.getName() != null && !courseDTO.getName().isEmpty(), "name", courseDTO.getName());
        queryWrapper.eq(courseDTO.getSubject() != null, "subject", courseDTO.getSubject());
        queryWrapper.eq(courseDTO.getTarget() != null, "target", courseDTO.getTarget());

        List<Course> courses = courseMapper.selectList(queryWrapper);

        PageInfo<Course> pageInfo = new PageInfo<>(courses);

        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public List<Course> findAllCourses() {
        return courseMapper.listAllCourses();
    }

    @Override
    public List<Course> listCoursesBySubject(Integer subject) {
        return courseMapper.selectBySubject(subject);
    }


}
